package com.cmsky.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmsky.model.CmsNode;
import com.cmsky.service.third.CmsQueryService;
import com.cmsky.utils.JsonUtils;

@Controller
@RequestMapping("/third")
public class ThridCmsQueryController {
	private static String className = ThridCmsQueryController.class.getName();
	private static Logger thirdCmsQueryLogger = Logger.getLogger(className);
	
	@Autowired
	private CmsQueryService cmsQueryService;

	/**
	 *  获取子节点列表
	 * @param request
	 * @param response
	 * @param pid
	 * @return json格式数据,负数表示异常，-1表示查询结果为null.
 	 * 	
	 */
	@RequestMapping("/p/{pid}")
	public @ResponseBody String findNodesByPid(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer pid){
		String result = null;
		List<CmsNode> nodeList = cmsQueryService.findNodesByPid(pid, false);
		if(null == nodeList){
			result = "-1";
		}else{
			result = JsonUtils.jsonFromObject(nodeList);
		}
		thirdCmsQueryLogger.info("<>"+className+"<|>findNodesByPid<|>"+pid+"<|>"+result+"<|>");
		return result;
	}
	
	/**
	 *  获取节点基本数据列表
	 * @param request
	 * @param response
	 * @param pid
	 * @return json格式数据,负数表示异常，-1表示查询结果为null.
 	 * 	
	 */
	@RequestMapping("/s/{id}")
	public @ResponseBody String findNodeById(HttpServletRequest request,HttpServletResponse response,@PathVariable Integer id){
		String result = null;
		CmsNode cmsNode = cmsQueryService.findNodeById(id, false);
		if(null == cmsNode){
			result = "-1";
		}else{
			result = JsonUtils.jsonFromObject(cmsNode);
		}
		thirdCmsQueryLogger.info("<>"+className+"<|>findNodesByPid<|>"+id+"<|>"+result+"<|>");
		return result;
	}
}
